package survivial;

import java.util.List;
import java.util.Random;

import tutorial.EventManager;
import tutorial.Tutorial;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

// based off BlockHalfSlab.. partly

public class BlockCampfire extends BlockContainer
{
	@SideOnly(Side.CLIENT)
	private Icon[] icons;

	@SideOnly(Side.CLIENT)
	public void registerIcons(IconRegister par1IconRegister)
	{
		icons = new Icon[4];
		String baseName = Survivial.modid + ":CampfireTransparent"; // + (this.getUnlocalizedName().substring(5));
		icons[0] = par1IconRegister.registerIcon(baseName);
		icons[1] = par1IconRegister.registerIcon(baseName + "Side");

		this.setBlockBounds(0.0F, -0.02F, 0.0F, 1.0F, -0.02F, 1.0F);
	}

	public boolean isOpaqueCube()
	{
		return false;
	}

	public boolean renderAsNormalBlock()
	{
		return false;
	}

	@SideOnly(Side.CLIENT)
	public Icon getIcon(int par1, int par2)
	{
		if (par1 == 0)
			return icons[0];
		else
			return icons[1];
		
		/*
		switch (par2)
		{
		case 0:
			return icons[0];
		case 1:
			{
				switch (par1)
				{
				case 0:
					return icons[0]; // top
				case 1:
					return icons[1]; // side
				default:
					return icons[1];
				}
			}
		default:
			{
				return icons[1];
				// System.out.println("Invalid metadata for " +
				// this.getUnlocalizedName());
				// return icons[0];
			}
		}
		*/
	}

	@SideOnly(Side.CLIENT)
	public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
	{
		for (int i = 0; i < 2; i++)
		{
			par3List.add(new ItemStack(par1, 1, i));
		}
	}

	/**
	 * Is the random generator used by campfire to drop the inventory contents
	 * in random directions.
	 */
	private final Random campfireRand = new Random();

	/** True if this is an active campfire, false if idle */
	private final boolean isActive;

	/**
	 * This flag is used to prevent the campfire inventory to be dropped upon
	 * block removal, is used internally when the campfire block changes from
	 * idle to active and vice-versa.
	 */
	private static boolean keepCampfireInventory;
	@SideOnly(Side.CLIENT)
	private Icon campfireIconTop;
	@SideOnly(Side.CLIENT)
	private Icon campfireIconFront;

	protected BlockCampfire(int par1, boolean par2)
	{
		super(par1, Material.wood);
		this.isActive = par2;
	}

	/**
	 * Returns the ID of the items to drop on destruction.
	 */
	@Override
	public int idDropped(int par1, Random par2Random, int par3)
	{
		return Survivial.campfireIdle.blockID;
	}

	/**
	 * Called whenever the block is added into the world. Args: world, x, y, z
	 */
	public void onBlockAdded(World par1World, int par2, int par3, int par4)
	{
		super.onBlockAdded(par1World, par2, par3, par4);
		this.setDefaultDirection(par1World, par2, par3, par4);
	}

	/**
	 * set a blocks direction
	 */
	private void setDefaultDirection(World par1World, int par2, int par3, int par4)
	{
		if (!par1World.isRemote)
		{
			int l = par1World.getBlockId(par2, par3, par4 - 1);
			int i1 = par1World.getBlockId(par2, par3, par4 + 1);
			int j1 = par1World.getBlockId(par2 - 1, par3, par4);
			int k1 = par1World.getBlockId(par2 + 1, par3, par4);
			byte b0 = 3;

			if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
			{
				b0 = 3;
			}

			if (Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
			{
				b0 = 2;
			}

			if (Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
			{
				b0 = 5;
			}

			if (Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
			{
				b0 = 4;
			}

			par1World.setBlockMetadataWithNotify(par2, par3, par4, b0, 2);
		}
	}

	// @SideOnly(Side.CLIENT)

	/**
	 * From the specified side and block metadata retrieves the blocks texture.
	 * Args: side, metadata public Icon getIcon(int par1, int par2) { return
	 * par1 == 1 ? this.campfireIconTop : (par1 == 0 ? this.campfireIconTop :
	 * (par1 != par2 ? this.blockIcon : this.campfireIconFront)); }
	 */

	// @SideOnly(Side.CLIENT)

	/**
	 * When this method is called, your block should register all the icons it
	 * needs with the given IconRegister. This is the only chance you get to
	 * register icons. public void registerIcons(IconRegister par1IconRegister)
	 * { this.blockIcon = par1IconRegister.registerIcon("campfire_side");
	 * this.campfireIconFront = par1IconRegister.registerIcon(this.isActive ?
	 * "campfire_front_on" : "campfire_front_off"); this.campfireIconTop =
	 * par1IconRegister.registerIcon("campfire_top"); }
	 */

	/**
	 * Called upon block activation (right click on the block.)
	 */
	public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
	{
		if (par1World.isRemote)
		{
			return true;
		}
		else
		{
			TileEntityCampfire tileentitycampfire = (TileEntityCampfire) par1World.getBlockTileEntity(par2, par3, par4);

			if (tileentitycampfire != null)
			{
				par5EntityPlayer.displayGUIFurnace(tileentitycampfire);
			}

			return true;
		}
	}

	/**
	 * Update which block ID the campfire is using depending on whether or not
	 * it is lit
	 */
	public static void updateCampfireBlockState(boolean par0, World par1World, int par2, int par3, int par4)
	{
		int l = par1World.getBlockMetadata(par2, par3, par4);
		TileEntity tileentity = par1World.getBlockTileEntity(par2, par3, par4);
		keepCampfireInventory = true;

		if (par0)
		{
			par1World.setBlock(par2, par3, par4, Survivial.campfireDimLow.blockID);
		}
		else
		{
			par1World.setBlock(par2, par3, par4, Survivial.campfireIdle.blockID);
		}

		keepCampfireInventory = false;
		par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2);

		if (tileentity != null)
		{
			tileentity.validate();
			par1World.setBlockTileEntity(par2, par3, par4, tileentity);
		}
	}

	static Random _rand = new Random();
	
	/**
	 * Returns a new instance of a block's tile entity class. Called on placing
	 * the block.
	 */
	public TileEntity createNewTileEntity(World par1World)
	{
		return new TileEntityCampfire();
	}

	/**
	 * Called when the block is placed in the world.
	 */
	public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLivingBase par5EntityLivingBase, ItemStack par6ItemStack)
	{
		int l = MathHelper.floor_double((double) (par5EntityLivingBase.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

		if (l == 0)
		{
			par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
		}

		if (l == 1)
		{
			par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
		}

		if (l == 2)
		{
			par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
		}

		if (l == 3)
		{
			par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
		}

		if (par6ItemStack.hasDisplayName())
		{
			((TileEntityCampfire) par1World.getBlockTileEntity(par2, par3, par4)).setGuiDisplayName(par6ItemStack.getDisplayName());
		}
	}

	/**
	 * Called on server worlds only when the block has been replaced by a
	 * different block ID, or the same block with a different metadata value,
	 * but before the new metadata value is set. Args: World, x, y, z, old block
	 * ID, old metadata
	 */
	public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
	{
		if (!keepCampfireInventory)
		{
			TileEntityCampfire tileentitycampfire = (TileEntityCampfire) par1World.getBlockTileEntity(par2, par3, par4);

			if (tileentitycampfire != null)
			{
				tileentitycampfire.IsDisabled = true;
				
				for (int j1 = 0; j1 < tileentitycampfire.getSizeInventory(); ++j1)
				{
					ItemStack itemstack = tileentitycampfire.getStackInSlot(j1);

					if (itemstack != null)
					{
						float f = this.campfireRand.nextFloat() * 0.8F + 0.1F;
						float f1 = this.campfireRand.nextFloat() * 0.8F + 0.1F;
						float f2 = this.campfireRand.nextFloat() * 0.8F + 0.1F;

						while (itemstack.stackSize > 0)
						{
							int k1 = this.campfireRand.nextInt(21) + 10;

							if (k1 > itemstack.stackSize)
							{
								k1 = itemstack.stackSize;
							}

							itemstack.stackSize -= k1;
							EntityItem entityitem = new EntityItem(par1World, (double) ((float) par2 + f), (double) ((float) par3 + f1),
									(double) ((float) par4 + f2), new ItemStack(itemstack.itemID, k1, itemstack.getItemDamage()));

							if (itemstack.hasTagCompound())
							{
								entityitem.getEntityItem().setTagCompound((NBTTagCompound) itemstack.getTagCompound().copy());
							}

							float f3 = 0.05F;
							entityitem.motionX = (double) ((float) this.campfireRand.nextGaussian() * f3);
							entityitem.motionY = (double) ((float) this.campfireRand.nextGaussian() * f3 + 0.2F);
							entityitem.motionZ = (double) ((float) this.campfireRand.nextGaussian() * f3);
							par1World.spawnEntityInWorld(entityitem);
						}
					}
				}

				par1World.func_96440_m(par2, par3, par4, par5);
			}
		}

		super.breakBlock(par1World, par2, par3, par4, par5, par6);
	}

	/**
	 * If this returns true, then comparators facing away from this block will
	 * use the value from getComparatorInputOverride instead of the actual
	 * redstone signal strength.
	 */
	public boolean hasComparatorInputOverride()
	{
		return true;
	}

	/**
	 * If hasComparatorInputOverride returns true, the return value from this is
	 * used instead of the redstone signal strength when this block inputs to a
	 * comparator.
	 */
	public int getComparatorInputOverride(World par1World, int par2, int par3, int par4, int par5)
	{
		return Container.calcRedstoneFromInventory((IInventory) par1World.getBlockTileEntity(par2, par3, par4));
	}

	@SideOnly(Side.CLIENT)
	/**
	 * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
	 */
	public int idPicked(World par1World, int par2, int par3, int par4)
	{
		return Survivial.campfireIdle.blockID;
	}
	 public void onNeighborBlockChange(World par1World, int par2, int par3, int par4, int par5)
	    {
	        if (!this.canBlockStay(par1World, par2, par3, par4))
	        {
	            //par1World.setBlockToAir(par2, par3, par4);
	            par1World.destroyBlock(par2, par3, par4, true);
	        }
	    }

	  
	    public boolean canBlockStay(World par1World, int par2, int par3, int par4)
	    {
	        return par1World.getBlockMaterial(par2, par3 - 1, par4).isSolid();
	    }
	    
	    @Override
	    public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
	   
	    {
	        return !super.canPlaceBlockAt(par1World, par2, par3, par4) ? false : this.canBlockStay(par1World, par2, par3, par4);
	    }
	    
}
